package com.xiao.community.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xiao.community.entity.GoodsEntity;
import com.xiao.community.entity.PintuanOrderEntity;
import com.xiao.community.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiao.community.entity.PintuanEntity;
import com.xiao.community.service.PintuanService;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.R;



/**
 * 
 *
 * @author 韩国涛
 * @email 18862701405@gmail.com
 * @date 2021-09-10 15:22:54
 */
@RestController
@RequestMapping("community/pintuan")
public class PintuanController {
    @Autowired
    private PintuanService pintuanService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("selectByGood/{pintuanId}")
    public PintuanEntity selectByGood(@PathVariable("pintuanId") Integer pintuanId){

        return pintuanService.selectByGood(pintuanId);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @RequestBody PintuanEntity pintuanEntity) throws IOException {
        PageUtils page = pintuanService.queryPage(params,pintuanEntity);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{pintuanId}")
    public R info(@PathVariable("pintuanId") Integer pintuanId){
		PintuanEntity pintuan = pintuanService.getById(pintuanId);

        return R.ok().put("pintuan", pintuan);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PintuanEntity pintuan){
		pintuanService.save(pintuan);

        GoodsEntity goodsEntity = goodsService.getById(pintuan.getGoodsId());

        System.out.println(goodsEntity.getGoodsIfsale());
        goodsEntity.setGoodsIfsale(1);
        goodsService.updateById(goodsEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PintuanEntity pintuan){
		pintuanService.updateById(pintuan);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] pintuanIds){
		pintuanService.removeByIds(Arrays.asList(pintuanIds));

        return R.ok();
    }

}
